﻿using Application.Features.Roles.Queries.GetAllByUserId;
using Application.Features.Users.Commands.Add;
using Application.Features.Users.Commands.Delete;
using Application.Features.Users.Commands.Update;
using Application.Features.Users.DTOs;
using Application.Features.Users.Queries.GetAll;
using Application.Features.Users.Queries.Pagination;
using Common.Models;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;

namespace WebAPI.Controllers;

/// <summary>
/// 用户管理
/// </summary>
[Description("用户管理")]
public class UserController : ApiControllerBase
{
    /// <summary>
    /// 全部查询
    /// </summary>
    /// <returns></returns>
    [HttpGet("query/all")]
    [Description("全部查询")]
    public async Task<Result<IEnumerable<UserOutput>>> GetAllUsersQueryAsync()
    {
        var data = await Mediator.Send(new GetAllUsersQuery());
        return await Result<IEnumerable<UserOutput>>.SucceedAsync(data);
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpPost("query/pagination")]
    [Description("分页查询")]
    public async Task<Result<PaginatedData<UserOutput>>> PaginationQueryAsync(UsersWithPaginationQuery query)
    {
        var data = await Mediator.Send(query);
        return await Result<PaginatedData<UserOutput>>.SucceedAsync(data);
    }

    /// <summary>
    /// 创建用户
    /// </summary>
    /// <param name="command">创建数据</param>
    /// <returns></returns>
    [HttpPost]
    [Description("创建")]
    public async Task<Result> AddAsync(AddUserCommand command)
    {
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改基本信息
    /// </summary>
    /// <param name="id">修改用户唯一标识</param>
    /// <param name="command">修改数据</param>
    /// <returns></returns>
    [HttpPut("{id}")]
    [Description("修改基本信息")]
    public async Task<Result> UpdateAsync(long id, UpdateUserCommand command)
    {
        command.UserId = id;
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="ids">删除多个请用英文下的逗号,分开</param>
    /// <returns></returns>
    [HttpDelete("delete/{ids}")]
    [Description("删除")]
    public async Task<Result> DeleteAsync(string ids)
    {
        await Mediator.Send(new DeleteUserCommand { UserIds = ids.Split(',').Select(long.Parse).ToList() });
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 分配角色
    /// </summary>
    /// <param name="userIds">用户唯一标识，多个请用英文下的逗号,分开</param>
    /// <param name="roleIds">角色唯一标识，可以为空;多个请用英文下的逗号,分开</param>
    /// <returns></returns>
    [HttpPut("{userIds}/assign/role/{roleIds?}")]
    [Description("分配角色")]
    public async Task<Result> AssignRoleToUserAsync(string userIds, string? roleIds)
    {
        await Mediator.Send(new AssignRoleToUserCommand
        {
            UserIds = userIds.Split(',').Select(long.Parse).ToList(),
            RoleIds = roleIds?.Split(',').Select(long.Parse).ToList(),
        });
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 查询用户角色
    /// </summary>
    /// <param name="userId">用户唯一标识</param>
    /// <returns></returns>
    [HttpPut("{userId}/role")]
    [Description("查询用户角色")]
    public async Task<Result> GetAllRolesByUserIdQueryAsync(long userId)
    {
        await Mediator.Send(new GetAllRolesByUserIdQuery
        {
            UserId = userId,
        });
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改用户状态
    /// </summary>
    /// <returns></returns>
    [HttpPut("{userId}/status")]
    [Description("修改状态")]
    public async Task<Result> UpdateStatusAsync(long userId, UpdateUserStatusCommand command)
    {
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改用户头像
    /// </summary>
    /// <returns></returns>
    [HttpPut("{userId}/avatar")]
    [Description("修改头像")]
    public async Task<Result> UpdateAvatarAsync(long userId, UpdateUserAvatarCommand command)
    {
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 重置密码
    /// </summary>
    /// <returns></returns>
    [HttpPut("{userId}/password")]
    [Description("重置密码")]
    public async Task<Result> ResetPasswordAsync(long userId, ResetPasswordCommand command)
    {
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }
}
